<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 22.03.14
 * Time: 1:23
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);

define("BASE_DQ_PLUGIN_PATH", dirname(__FILE__) . "/");
require(realpath(dirname(__FILE__) . "../../../../") . '/wp-blog-header.php');
require_once BASE_DQ_PLUGIN_PATH . "classes/QuizDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/QuestionDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/AnswerDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/ResultDao.php";

$quizDao = new QuizDao();
$questionDao = new QuestionDao();
$answerDao = new AnswerDao();
$resultDao = new ResultDao();

$quizId = intval($_REQUEST["id"]);
$studentId = wp_get_current_user()->ID;

$results = $resultDao->getAllByQuizUser($quizId, $studentId);
if (count($results) > 0) {
    header("Location: /quiz-result.php?id=" . $quizId);
    exit;
}

if (isset($_REQUEST["questions"]) && is_array($_REQUEST["questions"])) {
    //$resultDao->remove($quizId, $studentId);

    foreach ($_REQUEST["questions"] as $questionArr) {
        $result = new Result();
        $result->quizId = $quizId;
        $result->questionId = $questionArr["id"];
        $result->userId = $studentId;

        foreach ($questionArr["answers"] as $answer) {
            if (intval($answer) == $answer) {
                $result->answers[] = $answer;
            } elseif (is_array($answer)) {
                if ($answer["selected"] == "on") {
                    $result->answers[] = $answer["id"];
                }
            }
        }
        $resultDao->add($result);
    }

    header("Location: /quiz-result.php?id=" . $quizId);
}

// if not defined quiz id or quiz element not found redirect to index page
if (!isset($_GET["id"])
    || intval($_GET["id"]) <= 0
    || !$quizDao->getById(intval($_GET["id"]))
) {
    header("Location: /");
}

// check user permissions for this quiz
// TODO: check quiz

$quiz = $quizDao->getById(intval($_GET["id"]));
$quiz->questions = $questionDao->getAllByQuizId($quiz->id);
shuffle($quiz->questions);

foreach ($quiz->questions as $question) {
    $question->answers = $answerDao->getAllByQuestionId($question->id);
    shuffle($question->answers);
}

// Fix for page title
$wp_query->is_404 = false;
?>

<?php get_header("header-name.php"); ?>

<? include_once BASE_DQ_PLUGIN_PATH . "header.php"; ?>

    <div id="content" class="left">
        <h2 class="entry-title">
            <b><?= $quiz->name ?></b>
        </h2>

        <form method="post">
            <input type="hidden" name="id" value="<?= $quiz->id ?>"/>
            <table>
                <tbody>
                <? $indexQuestion = 0; ?>
                <? foreach ($quiz->questions as $question) { ?>
                    <? $answersCorrectCount = $question->getCorrectAnswersCount(); ?>
                    <? if ($question->getAnswersCount() > 0) { ?>
                        <tr>
                            <td>
                                <input type="hidden"
                                       name="questions[<?= $indexQuestion ?>][id]"
                                       value="<?= $question->id ?>"/>
                                <?= $question->content ?>
                            </td>
                            <td>
                                <ul>
                                    <? $indexAnswer = 0; ?>
                                    <? foreach ($question->answers as $answer) { ?>
                                        <li>
                                            <? if ($answersCorrectCount == 1) { ?>
                                                <input type="radio"
                                                       name="questions[<?=
                                                       $indexQuestion
                                                       ?>][answers][]"
                                                       value="<?= $answer->id ?>"/>
                                            <? } else { ?>
                                                <input type="hidden"
                                                       name="questions[<?=
                                                       $indexQuestion
                                                       ?>][answers][<?=
                                                       $indexAnswer
                                                       ?>][id]"
                                                       value="<?= $answer->id ?>"/>
                                                <input type="checkbox"
                                                       name="questions[<?=
                                                       $indexQuestion
                                                       ?>][answers][<?=
                                                       $indexAnswer
                                                       ?>][selected]"/>
                                            <? } ?>
                                            <span><?= $answer->content ?></span>
                                        </li>
                                        <? $indexAnswer++; ?>
                                    <? } ?>
                                </ul>
                            </td>
                        </tr>
                    <? } ?>
                    <? $indexQuestion++; ?>
                <? } ?>
                </tbody>
            </table>
            <input type="submit" value="Завершить тест"
                   onclick="return confirm('После завершения теста изменить ответы будет невозможно. Хотите продолжить?');"/>
        </form>
    </div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>